using System;
using Glue;
using Xunit;

namespace GlueTests.MapperTests
{
    public class When_mapping_a_nested_relation
    {
        public class GuiPerson
        {
            public String Name { get; set; }
            public GuiAddress Address { get; set; }
        }

        public class GuiAddress
        {
            public String City { get; set; }
        }

        public class DomainPerson
        {
            public String Name { get; set; }
            public DomainAddress Address { get; set; }
        }

        public class DomainAddress
        {
            public String City { get; set; }
        }

        private readonly Mapping<DomainPerson, GuiPerson> personMapping;

        public When_mapping_a_nested_relation()
        {
            var addressMap = new Mapping<DomainAddress, GuiAddress>();
            addressMap.AutoRelateEqualNames();
            personMapping = new Mapping<DomainPerson, GuiPerson>();
            personMapping.AddConverter(addressMap);
            personMapping.AutoRelateEqualNames();
        }

        [Fact]
        public void Should_map_nested_relation_towards_right()
        {
            var domainPerson = new DomainPerson {Address = new DomainAddress {City = "Nested city name"}};
            var guiPerson = new GuiPerson {Address = new GuiAddress()};
            personMapping.Map(domainPerson, guiPerson);
            Assert.Equal(domainPerson.Address.City,guiPerson.Address.City);
        }

        [Fact]
        public void Should_map_nested_relation_towards_left()
        {
            var guiPerson = new GuiPerson { Address = new GuiAddress{City = "Nested city name"} };
            var domainPerson = new DomainPerson { Address = new DomainAddress() };

            personMapping.Map(guiPerson, domainPerson);
            Assert.Equal(guiPerson.Address.City, domainPerson.Address.City);
        }
    }
}